import { _decorator, Component, Node, tween, UIOpacity } from 'cc';
import { NormalButton } from './NormalButton';
const { ccclass, property } = _decorator;
// TODO 47最后几分钟，48一节，都没做
@ccclass('Chartpanel')
export class Chartpanel extends Component {
    @property(Node) ndBtnLeft: Node;
    @property(Node) ndBtnRight: Node;
    @property(Node) ndChars: Node;


    private _curIndex: number = 0; // 当前选中第几个

    protected onEnable(): void {
        this.ndBtnLeft.getComponent(NormalButton).onClick(()=> {
            this.moveLeft();
            this.updateCharItemState();
        })

        this.ndBtnRight.getComponent(NormalButton).onClick(()=> {
            this.moveRight();
            this.updateCharItemState();
        })
        this._curIndex = 0;
        this.updateCharItemState();
    }

    protected onDisable(): void {
        
    }


    start() {

    }

    update(deltaTime: number) {
        
    }

    gotoIndex(index:number) {
        // TODO 47节课最后几分钟
    }

    updateCharItemState() {
        for(let i = 0; i<this.ndChars.children.length;i++) {
            this.ndChars.children[i].getComponent(UIOpacity).opacity = i === this._curIndex ? 255 : 80;
        }

        this.ndBtnLeft.active = this._curIndex > 0;
        this.ndBtnRight.active = this._curIndex < this.ndChars.children.length - 1;
    }

    moveRight() {
        const len = this.ndChars.children.length;
        this._curIndex++;
        if(this._curIndex >= len) {
            this._curIndex = len - 1;
            return
        }
        for(let i=0;i<len;i++) {
            const child = this.ndChars.children[i];
            const pos = child.position.clone();
            tween(child)
                .to(0.4, {position: pos.add3f(-174,0,0)}, {easing: 'expoOut'})
                .start();
        }

    }

    moveLeft() {
        const len = this.ndChars.children.length;
        this._curIndex--;
        if(this._curIndex < 0) {
            this._curIndex = 0;
            return
        }
        for(let i=0;i<len;i++) {
            const child = this.ndChars.children[i];
            const pos = child.position.clone();
            tween(child)
                .to(0.4, {position: pos.add3f(174,0,0)}, {easing: 'expoOut'})
                .start();
        }
    }
}


